//
//  ARYViewController.m
//  aryshinKit
//
//  Created by aryshin2016@hotmail.com on 05/10/2017.
//  Copyright (c) 2017 aryshin2016@hotmail.com. All rights reserved.
//

#import "ARYViewController.h"
#import <objc/message.h>
@import GameKit;

#import "ARYKeyChainEX.h"

@interface ARYViewController ()

@property (nonatomic, strong) UIImageView *iviev;

@end

@implementation ARYViewController

-(UIImageView *)iviev {
    if (_iviev ==nil) {
        // 测试加载资源图片的对象方法（无参数）
        Class TestImage = NSClassFromString(@"TestImage");
        if (TestImage ==nil) {
            NSLog(@"动态加载TestImage类失败！！！！！！！！！！！！！");
            return _iviev;
        }
        id instance = [[TestImage alloc] init];
        SEL selector = NSSelectorFromString(@"loadPic");
        if (![instance respondsToSelector:selector]) {
            return _iviev;
        }
#warning 这种写法的原理是什么？？？？？
        IMP imp = [instance methodForSelector:selector];
        UIImage *(*func)(id, SEL) = (void *)imp;
        UIImage *image = func(instance, selector);
        _iviev = [[UIImageView alloc] initWithImage:image];
        
    }
    return _iviev;
}

- (IBAction)loadImage {
    
    [self.view addSubview:self.iviev];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // 测试加载资源图片的类方法，且带参数
    Class TestImage = NSClassFromString(@"TestImage");
    if (TestImage ==nil) {
        NSLog(@"动态加载TestImage类失败！！！！！！！！！！！！！");
        return;
    }
    //id instance = [[TestImage alloc] init];
    SEL selector = NSSelectorFromString(@"ar_imageNamed:");
    if (![TestImage respondsToSelector:selector]) {
        return;
    }else {
//        NSLog(@"%zd",[TestImage respondsToSelector:selector]);
        UIImage *image = objc_msgSend(TestImage, selector,@"aaaaaaaa");
        UIImageView *view = [[UIImageView alloc] initWithImage:image];
        [self.view addSubview:view];
        view.center = CGPointMake([UIScreen mainScreen].bounds.size.width *0.5, [UIScreen mainScreen].bounds.size.width *0.5);
    }
    
}


/**
 新评分
 */
- (IBAction)ratings {
    Class SKrequestReview = NSClassFromString(@"SKrequestReview");
    if (SKrequestReview ==nil) {
        NSLog(@"动态加载SKrequestReview类失败！！！！！！！！！！！！！");
        return;
    }
    id instance = [[SKrequestReview alloc] init];
    SEL selector = NSSelectorFromString(@"Ratings");
    if ([instance respondsToSelector:selector]) {
        
        [instance performSelector:selector withObject:nil afterDelay:0.0];
    }
}

/**
 测试gamecenter登录
 */
- (IBAction)ARGameCenter {
    Class arClass = NSClassFromString(@"ARGameCenter");
    if (arClass ==nil) {
        NSLog(@"动态加载ARGameCenter失败！！！！！！");
        return;
    }
    id ar = [arClass new];
    if (![ar respondsToSelector:NSSelectorFromString(@"checkGameCenterAvailability")]) {
        NSLog(@"ARGameCenter没有相应的checkGameCenterAvailability方法！！！！！！");
        return;
    }
    BOOL canAR = [ar performSelector:NSSelectorFromString(@"checkGameCenterAvailability")];
    if (canAR) {
        // 调用gamecenter登录
        //[self instanceloginWithGameCenter:ar];
        [self gameCenterLoginHander:ar];
    }
}
// 调用gamecenter登录 不需要处理登录结果
- (void)instanceloginWithGameCenter: (id)instance{
    if (![instance respondsToSelector:NSSelectorFromString(@"localGameCenterLogin")]) {
        NSLog(@"ARGameCenter没有相应的localGameCenterLogin方法！！！！！！");
    }else {
        [instance performSelector:NSSelectorFromString(@"localGameCenterLogin")];
    }
    
}
// 调用gamecenter登录 需要登录结果
- (void)gameCenterLoginHander:(id)instance {
    
    if (![instance respondsToSelector:NSSelectorFromString(@"localGameCenterLoginComplihander:")]) {
        NSLog(@"ARGameCenter没有相应的localGameCenterLogin方法！！！！！！");
    }else {
        [instance performSelector:NSSelectorFromString(@"localGameCenterLoginComplihander:") withObject:^(GKLocalPlayer *localPlayer, NSError *error) {
            NSLog(@"%@",error);
            NSLog(@"%@",[localPlayer playerID]);
        }];
    }
}
- (IBAction)dicRuntime {
    
    id nilObj  = nil;
    //1
//    NSDictionary *dic = @{@"qw":@12,@"w":nilObj};
//    NSLog(@"------------%@", dic);
    //2
//    NSMutableDictionary *mudic = [NSMutableDictionary dictionary];
//    [mudic setObject:nilObj forKey:@"aaaaa"];
    // 3. 此处会引起崩溃
//    NSArray *array = @[@"aa", nilObj];
//    NSLog(@"array: %@", array);
    
    
    // 4. 此处会引起崩溃
    NSMutableArray *mArray = [NSMutableArray array];
    mArray[0] = @123;
    mArray[1] = nilObj;
    
    // 5. 此处会引起崩溃
//    [mArray addObject:nilObj];
//    [mArray addObject:@"bbb"];
    NSLog(@"mArray: %@", mArray);
}
    
#pragma mark - KeyChain use test
- (IBAction)readKeyChain {
    NSString *bundleid = [NSBundle mainBundle].bundleIdentifier;
    NSString *idfa = [ARYKeyChainEX readKeyChainWithIdenty:bundleid];
    NSLog(@"=====================读取KeyChain中的idfa标志:%@", idfa);
}
- (IBAction)saveKeyChain {
    NSString *bundleid = [NSBundle mainBundle].bundleIdentifier;
    NSString *idfa = [UIDevice currentDevice].identifierForVendor.UUIDString;
    [ARYKeyChainEX saveKeyChainWithIdenty:bundleid data:idfa];
}

@end
